﻿Imports TBCC.Utility

''' <summary>
''' 制冷设备类型服务类
''' </summary>
''' <remarks></remarks>
Public Class CcapDevItemService

    ''' <summary>
    ''' 默认构造函数
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()

    End Sub
    Public Shared Function GetCcapDevTypeList(ByVal pi As ProjectItem) As IList(Of CcapDevItem)
        Dim dtil As IList(Of CcapDevItem)
        dtil = New List(Of CcapDevItem)

        Dim strSql As String
        strSql = "Select * From TbccCcapDevType;Select * From tbccCompressorSet;Select * From tbcccompressor;Select * From tbcccondenser;Select * From tbccaircooler"

        Dim ds As DataSet
        ds = TBCC.Utility.SqlHelper.ExecuteDataset(strSql, Nothing)

        '查询错误
        If ds Is Nothing OrElse ds.Tables.Count = 0 Then
            Return Nothing
        End If

        Dim filter1 As String
        filter1 = String.Format("ProjectID='{0}'", pi.ProjectID)

        For Each r1 As DataRow In ds.Tables(0).Select(filter1) '对每一个设备
            Dim dti As New CcapDevItem()
            dti.Id = r1("id")
            dti.ProjectID = r1("projectID")
            dti.NetId = r1("csNetid")
            dti.DevType = r1("DevType")

            Dim filter2 As String
            filter2 = String.Format(" Devid='{0}'", dti.Id)

            For Each r2 As DataRow In ds.Tables(1).Select(filter2) '对每一个机组
                Dim rti As New CcapCompressorSetItem()
                rti.Id = r2("id")
                rti.ProjectID = dti.ProjectID
                rti.devtId = r2("Devid")
                rti.NetId = dti.NetId
                rti.CompressorSetName = r2("csName")
                rti.CompressorSetType = r2("csType")
                rti.CompressorCount = r2("csCount")
                rti.RelId = r2("RelId")
                dti.CompressorSetGroupList.Add(rti)

                Dim filter3 As String
                filter3 = String.Format("csId='{0}' ", rti.Id)
                For Each r3 As DataRow In ds.Tables(2).Select(filter3)   '每个压缩机
                    Dim com As New CcapCompressorItem
                    com.Id = r3("id")
                    com.NetId = rti.NetId
                    com.CompressorName = r3("name")
                    com.CsId = r3("csId")
                    com.RelId = r3("RelId")
                    rti.CompressorGroupList.Add(com)
                Next

               

           
               

            Next

            Dim filter4 As String
            filter4 = String.Format(" csNetid='{0}'  ", dti.NetId)
            For Each r4 As DataRow In ds.Tables(3).Select(filter4)
                Dim con As New CcapCondenserItem

                con.Id = r4("id")
                con.ProjectID = dti.ProjectID
                con.NetId = dti.NetId
                con.CsId = r4("csId")
                con.CondenserName = r4("name")
                con.RelId = r4("RelId")
                dti.CondenserGroupList.Add(con)

            Next
            For Each r5 As DataRow In ds.Tables(4).Select(filter4)
                Dim air As New CcapAirCoolerItem

                air.Id = r5("id")
                air.ProjectID = dti.ProjectID
                air.NetId = dti.NetId
                air.CsId = r5("csId")
                air.AirCoolerName = r5("name")
                air.RelId = r5("RelId")
                dti.AirCoolerGroupList.Add(air)

            Next


            dtil.Add(dti)
        Next

        Return dtil

    End Function

    Public Shared Function AddCCapDevList(ByVal item As CcapDevItem) As Boolean
        Dim strSql As String
        Dim tic As New TBCC.Utility.TranstrationItem
        Dim index As Integer
        Dim obj As Object
        '获取最大的CCAPDEV ID
        strSql = String.Format("select max(id) from tbccccapdevtype")
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)
        Dim CCapDevId As Integer
        If obj Is Nothing OrElse IsDBNull(obj) Then
            CCapDevId = 0
        Else
            CCapDevId = CLng(obj)
        End If
        CCapDevId += 1

        '获取机组的最大ID
        strSql = String.Format("select max(id) from tbccCompressorSet")
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)
        Dim tcsId As Integer
        If obj Is Nothing OrElse IsDBNull(obj) Then
            tcsId = 0
        Else
            tcsId = CLng(obj)
        End If
        tcsId += 1

        '获取最大的冷凝器 ID
        strSql = String.Format("select max(id) from tbccCondenser")
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)
        Dim tconId As Integer
        If obj Is Nothing OrElse IsDBNull(obj) Then
            tconId = 0
        Else
            tconId = CLng(obj)
        End If
        tconId += 1

        '获取最大的冷风机 ID
        strSql = String.Format("select max(id) from tbccAirCooler")
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)
        Dim tacId As Integer
        If obj Is Nothing OrElse IsDBNull(obj) Then
            tacId = 0
        Else
            tacId = CLng(obj)
        End If
        tacId += 1

        '获取最大的压缩机 ID
        strSql = String.Format("select max(id) from tbccCompressor")
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)
        Dim tcId As Integer
        If obj Is Nothing OrElse IsDBNull(obj) Then
            tcId = 0
        Else
            tcId = CLng(obj)
        End If
        tcId += 1


        '    For Each item As CcapDevItem In pi.CcapDevTypeGroupList
        '        If pi.CcapCommonItem.CcapId = item.Id Then
        '            pi.CcapCommonItem.CcapId = CCapDevId
        '        End If
        item.Id = CCapDevId
        '        CCapDevId += 1

        '增加制冷设备表信息
        strSql = String.Format("Insert into TbccCcapDevType(id,projectID,csNetid,DevType) Values({0},'{1}',{2},{3}) -- " & index, item.Id, item.ProjectID, item.NetId, item.DevType)
        tic.Add(strSql, Nothing)
        index += 1

        '        If pi.CcapCommonItem.CcapId = item.Id Then
        '            'pi.CcapCommonItem.NetId = CCapDevId
        '            strSql = String.Format("Insert into tbccCcapPortSet(devId,CpsLostPowerAlarmPort,CpsBreakDownAlarmPort) values({0},{1},{2}) -- " & index, pi.CcapCommonItem.CcapId, pi.CcapCommonItem.CcapLostPowerAlarmPort, pi.CcapCommonItem.CcapGeneratorStartAlarmPort)
        '            tic.Add(strSql, Nothing)
        '            index += 1
        '        End If
       



        For Each cs As CcapCompressorSetItem In item.CompressorSetGroupList   '机组表
            'For Each item_temp As CcapDevItem In item.CcapDevTypeGroupList
            For Each cd As CcapCondenserItem In item.CondenserGroupList    '冷凝器
                If cd.CsId = cs.Id Then
                    cd.CsId = tcsId
                End If
            Next
            For Each ac As CcapAirCoolerItem In item.AirCoolerGroupList   '冷风机
                If ac.CsId = cs.Id Then
                    ac.CsId = tcsId
                End If
            Next
            'Next

            cs.Id = tcsId
            tcsId += 1

            strSql = String.Format("Insert into tbccCompressorSet(id,Devid,csName,csType,csCount,RelId) values('{0}','{1}','{2}','{3}','{4}','{5}') -- " & index, cs.Id, item.Id, cs.CompressorSetName, cs.CompressorSetType, cs.CompressorCount, cs.RelId)
            tic.Add(strSql, Nothing)
            index += 1

            '            '机组端口配置信息表
            '            If cs.MultiCompressorSetPort IsNot Nothing Then
            '                strSql = String.Format("Insert into tbccMultiCompressorSetPortSet(csid,CsSuckPressAlarmPort,CsLowLiquidAlarmPort,CsLostPowerAlarmPort) values({0},{1},{2},{3}) -- " & index, cs.Id, cs.MultiCompressorSetPort.CsSuckPressAlarmPort, cs.MultiCompressorSetPort.CsLowLiquidAlarmPort, cs.MultiCompressorSetPort.CsLostPowerAlarmPort)
            '                tic.Add(strSql, Nothing)
            '                index += 1
            '            End If

            '            '冷凝机组端口配置信息表
            '            If cs.SingleCompressorSetPort IsNot Nothing Then
            '                strSql = String.Format("Insert into tbccSingleCompressorPortSet(csid,CsLostPowerAlarmPort,CsOverLoadAlarmPort,CsBreakDownAlarmPort) values({0},{1},{2},{3}) -- " & index, cs.Id, cs.SingleCompressorSetPort.CsLostPowerAlarmPort, cs.SingleCompressorSetPort.CsOverLoadAlarmPort, cs.SingleCompressorSetPort.CsBreakDownAlarmPort)
            '                tic.Add(strSql, Nothing)
            '                index += 1
            '            End If



            For Each cp As CcapCompressorItem In cs.CompressorGroupList   '压缩机信息表
                cp.Id = tcId
                tcId += 1
                '压缩机机头
                strSql = String.Format("Insert into tbccCompressor(id,csId,name,RelId) values('{0}','{1}','{2}','{3}') -- " & index, cp.Id, cs.Id, cp.CompressorName, cp.RelId)
                tic.Add(strSql, Nothing)
                index += 1

                '                '压缩器端口信息
                '                strSql = String.Format("Insert into tbccCompressorPortSet(cpId,cpLowPressAlarmPort,cpHighPressAlarmPort,cpOileAlarmPort,cpChargeTAlarmPort,cpOverLoadAlarmPort,cpActiveStatePort) values({0},{1},{2},{3},{4},{5},{6}) -- " & index, cp.Id, cp.CompressorPort.cpLowPressAlarmPort, cp.CompressorPort.cpHighPressAlarmPort, cp.CompressorPort.cpOileAlarmPort, cp.CompressorPort.cpChargeTAlarmPort, cp.CompressorPort.cpOverLoadAlarmPort, cp.CompressorPort.cpActiveStatePort)
                '                tic.Add(strSql, Nothing)
                '                index += 1
            Next
        Next

        For Each cd As CcapCondenserItem In item.CondenserGroupList    '冷凝器

            cd.Id = tconId
            tconId += 1

            strSql = String.Format("Insert into tbccCondenser(id,csNetid,csId,name,RelId) values('{0}','{1}','{2}','{3}','{4}') -- " & index, cd.Id, item.NetId, cd.CsId, cd.CondenserName, cd.RelId)
            tic.Add(strSql, Nothing)
            index += 1

            'strSql = String.Format("Insert into tbccCondenserPortSet(ceId,cePressPreAlarmPort) values({0},{1}) -- " & index, cd.Id, cd.CondenserPort.cePressPreAlarmPort)
            'tic.Add(strSql, Nothing)
            'index += 1
        Next


        For Each ac As CcapAirCoolerItem In item.AirCoolerGroupList   '冷风机
            ac.Id = tacId
            tacId += 1

            strSql = String.Format("Insert into tbccAirCooler(id,csNetid,csId,name,RelId) values('{0}','{1}','{2}','{3}','{4}') -- " & index, ac.Id, item.NetId, ac.CsId, ac.AirCoolerName, ac.RelId)
            tic.Add(strSql, Nothing)
            index += 1

            'strSql = String.Format("Insert into tbccAirCoolerPortSet(acId,acDefPreAlarmPort) values({0},{1}) -- " & index, ac.Id, ac.AirCoolerPort.acDefPreAlarmPort)
            'tic.Add(strSql, Nothing)
            'index += 1
        Next

        '  Next

        '2010年05月17日更新，支持多工程导入配置

        'strSql = "Insert into TbccCcapDevType(id,projectID,csNetid,DevType) Values(?p0,?p1,?p2,?p3) -- " & index
        'Dim p1(3) As System.Data.Common.DbParameter

        'p1(0) = New MySql.Data.MySqlClient.MySqlParameter()
        'p1(0).ParameterName = "p0" : p1(0).Value = item.Id

        'p1(1) = New MySql.Data.MySqlClient.MySqlParameter()
        'p1(1).ParameterName = "p1" : p1(1).Value = item.ProjectID

        'p1(2) = New MySql.Data.MySqlClient.MySqlParameter()
        'p1(2).ParameterName = "p2" : p1(2).Value = item.NetId

        'p1(3) = New MySql.Data.MySqlClient.MySqlParameter()
        'p1(3).ParameterName = "p3" : p1(3).Value = item.DevType

        'tic.Add(strSql, p1)
        'index += 1

        'For Each cs As CcapCompressorSetItem In item.CompressorSetGroupList   '机组表

        '    strSql = String.Format("Insert into tbccCompressorSet(id,Devid,csName,csType,csCount) values('{0}','{1}','{2}','{3}','{4}') -- " & index, cs.Id, item.Id, cs.CompressorSetName, cs.CompressorSetType, cs.CompressorCount)
        '    tic.Add(strSql, Nothing)
        '    index += 1
        '    For Each cp As CcapCompressorItem In cs.CompressorGroupList   '压缩机信息表
        '        strSql = String.Format("Insert into tbccCompressor(id,csId,name) values('{0}','{1}','{2}') -- " & index, cp.Id, cs.Id, cp.CompressorName)
        '        tic.Add(strSql, Nothing)
        '        index += 1
        '    Next


        'Next


        'For Each cd As CcapCondenserItem In item.CondenserGroupList
        '    strSql = String.Format("Insert into tbccCondenser(id,csNetid,csId,name) values('{0}','{1}','{2}','{3}') -- " & index, cd.Id, item.NetId, cd.CsId, cd.CondenserName)
        '    tic.Add(strSql, Nothing)
        '    index += 1

        'Next

        'For Each ac As CcapAirCoolerItem In item.AirCoolerGroupList
        '    strSql = String.Format("Insert into tbccAirCooler(id,csNetid,csId,name) values('{0}','{1}','{2}','{3}') -- " & index, ac.Id, item.NetId, ac.CsId, ac.AirCoolerName)
        '    tic.Add(strSql, Nothing)
        '    index += 1

        'Next

        Return TBCC.Utility.SqlHelper.ExecuteTranstration(tic)




    End Function

    ''' <summary>
    ''' 获取当前工程中最大的netid
    ''' </summary>    
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DevTypeMaxNetid(ByVal pi As String) As Integer
        Dim strSql As String
        strSql = "Select max(csNetid) from TbccCcapDevType where projectID=?p0"

        Dim p(0) As MySql.Data.MySqlClient.MySqlParameter
        p(0) = New MySql.Data.MySqlClient.MySqlParameter
        p(0).ParameterName = "p0" : p(0).Value = pi

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, p)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return 49
        Else
            Return CInt(obj)
        End If


    End Function
    ''' <summary>
    ''' 获取当前工程中最大的id
    ''' </summary>    
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function DevTypeMaxid(ByVal pi As String) As Integer
        Dim strSql As String
        strSql = "Select max(id) from TbccCcapDevType "

        'Dim p(0) As MySql.Data.MySqlClient.MySqlParameter
        'p(0) = New MySql.Data.MySqlClient.MySqlParameter
        'p(0).ParameterName = "p0" : p(0).Value = pi

        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return 0
        Else
            Return CInt(obj)
        End If


    End Function
    ''' <summary>
    '''  返回设备类型 1 8DI  2 24DI
    ''' </summary>
    ''' <param name="Pid"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function GetCcapDevType(ByVal Pid As String) As Integer
        Dim strSql As String
        strSql = String.Format("Select DevType from TbccCcapDevType where projectID='{0}' and csNetid=50", Pid)
        'Dim p(0) As MySql.Data.MySqlClient.MySqlParameter
        'p(0) = New MySql.Data.MySqlClient.MySqlParameter
        'p(0).ParameterName = "p0" : p(0).Value = Pid
        Dim obj As Object
        obj = SqlHelper.ExecuteScalar(strSql, Nothing)

        If obj Is Nothing OrElse IsDBNull(obj) Then
            Return 0
        Else
            Return CInt(obj)
        End If

    End Function

    '为设备类型配置提供删除功能
    'netcount:剩余的设备的数量
    Public Shared Function DelCcapDevRefTypeList(ByVal item As CcapDevItem, ByVal netcount As Integer) As Boolean
        Dim strSql As String

        Dim tic As New TBCC.Utility.TranstrationItem

        strSql = String.Format("delete from tbccCondenser where csNetid={0}", item.NetId)
        tic.Add(strSql, Nothing)

        strSql = String.Format("delete from tbccAirCooler where  csNetid={0}", item.NetId)
        tic.Add(strSql, Nothing)

        strSql = String.Format("delete from TbccCcapDevType where projectID='{0}' and csNetid={1}", item.ProjectID, item.NetId)
        tic.Add(strSql, Nothing)


        Return TBCC.Utility.SqlHelper.ExecuteTranstration(tic)
    End Function



    ''' <summary>
    ''' 获取项目的机组列表
    ''' </summary>
    ''' <param name="projectId">项目编号</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetProjectDevices(ByVal projectId As String) As DataSet
        Dim ds As DataSet = Nothing
        Dim strSql As String = String.Empty

        strSql = " SELECT CS.Id AS Id,CS.DevId AS DevId,CS.csName AS csName,CS.csType AS csType,CS.csCount AS csCount,Dev.csNetId AS csNetId " + _
                " FROM tbcccompressorset AS CS Inner Join tbccccapdevtype AS Dev ON CS.DevId = Dev.Id  " + _
                 "WHERE Dev.ProjectId =  '" + projectId + "'  AND Dev.csNetId >=  50 Order By csNetId ASC"
        Try
            ds = SqlHelper.ExecuteDataset(strSql, Nothing)
        Catch ex As Exception
            ds = Nothing
        End Try

        Return ds
    End Function

    ''' <summary>
    ''' 获取项目的网络设备数量
    ''' </summary>
    ''' <param name="projectId">项目编号</param>
    ''' <returns>冷凝相关设备数量</returns>
    ''' <remarks></remarks>
    Public Shared Function GetNetDevicuesCount(ByVal projectId As String) As Integer
        Dim strSql As String
        strSql = "Select count(Id) From tbccccapdevtype Where ProjectId='" + projectId + "' And csNetId >=50 "

        Dim icount As Integer = 0
        Try
            icount = CInt(SqlHelper.ExecuteScalar(strSql, Nothing))
        Catch ex As Exception
            icount = 0
        End Try

        Return icount
    End Function
End Class
